//
//  PlatformView.swift
//  d11
//
//  Created by peale on 2018/11/19.
//  Copyright © 2018年 Tech_001. All rights reserved.
//

import UIKit
typealias PlatformSelectBlock = (_ index: Int) -> Void
class PlatformView: UIView,UITableViewDelegate,UITableViewDataSource{
    let  reuseIdentifierID : String = "PlatformViewCell"
    var platformSelectBlock: PlatformSelectBlock?
    func callClickBlock(_ block: @escaping PlatformSelectBlock) {
        platformSelectBlock = block
    }
    var arrayList : [[String:Any]]? //数据列表
    var type : Int = 0
    var selectIndex = 0
    var selectDict : [String:Any]?
    var bgImageView : UIImageView?
    var topBtn : UIButton?
    var topImageView : FLAnimatedImageView?
    var bottomBtn :UIButton?
     var bottomImageView : FLAnimatedImageView?
    lazy var tableView:UITableView = {
        let table = UITableView(frame:CGRect(x: 6, y: 24, width: self.width-12, height:self.height-48), style:UITableView.Style.plain)
        table.dataSource = self
        table.delegate = self
        //table.layoutMargins = UIEdgeInsets.zero
        table.separatorStyle = .none
        table.backgroundColor  = UIColor.clear
        table.showsVerticalScrollIndicator = false
        table.tableFooterView = UIView.init()
        table.separatorStyle = .none
        table.autoresizingMask = [.flexibleWidth,.flexibleHeight]
        table.estimatedRowHeight = 0
        table.estimatedSectionHeaderHeight = 0
        table.estimatedSectionFooterHeight = 0
        return table
    }()
    convenience init(list:[[String:Any]],frame:CGRect) {
        self.init(frame: frame)
        let platforms = GlobalInfo.platformInfoData
        self.arrayList =  platforms
        self.backgroundColor = UIColor.clear
        self.bgImageView = UIImageView.init()
        self.bgImageView?.isUserInteractionEnabled = true
        self.bgImageView?.image = imageNamed("icon_home_platform_bg")
        self.addSubview(self.bgImageView!)
        self.bgImageView?.snp.makeConstraints{(make) in
            make.width.equalToSuperview()
            make.height.equalToSuperview()
            make.left.equalToSuperview()
            make.top.equalToSuperview()
        }
        
        self.addSubview(self.tableView)
        
        self.topBtn = UIButton.init(type: .custom)
       // self.topBtn?.setImage(imageNamed("home_platform_top"), for: .normal)
       // self.topBtn?.backgroundColor = UIColor.white
        self.topBtn?.isHidden = true
        self.topBtn?.addTarget(self, action: #selector(actionToScrollTop), for: UIControl.Event.touchUpInside)
        self.bgImageView?.addSubview(self.topBtn!)
        self.topBtn?.snp.makeConstraints{(make) in
            make.width.equalToSuperview().offset(-12)
            make.height.equalTo(24)
            make.centerX.equalToSuperview()
            make.top.equalTo(3)
        }
        
        self.topImageView = FLAnimatedImageView.init()
        let url =  Bundle.main.url(forResource: "home_platform_top_gif", withExtension: "gif")
        do{
            let data = try Data(contentsOf: url!)
            let animatedImage = FLAnimatedImage.init(animatedGIFData: data)
            self.topImageView?.animatedImage = animatedImage
        }catch{}
      
        self.self.topBtn?.addSubview(self.topImageView!)
        self.topImageView?.snp.makeConstraints{(make) in
            make.width.equalTo(25)
            make.height.equalTo(14)
            make.centerX.equalToSuperview()
            make.top.equalTo(6)
        }
        self.bottomBtn = UIButton.init(type: .custom)
       // self.bottomBtn?.setImage(imageNamed("home_platform_bottom"), for: .normal)
      //  self.bottomBtn?.setImage(UIImage.gif(name: "home_platform_bottom_gif"), for: .normal)
        self.bottomBtn?.addTarget(self, action: #selector(actionToScrollBottom), for: UIControl.Event.touchUpInside)
     //   self.bottomBtn?.backgroundColor = UIColor.white
        self.bgImageView?.addSubview(self.bottomBtn!)
        self.bottomBtn?.snp.makeConstraints{(make) in
            make.width.equalToSuperview().offset(-12)
            make.height.equalTo(24)
            make.centerX.equalToSuperview()
            make.bottom.equalToSuperview().offset(-3)
        }
        
        self.self.bottomImageView = FLAnimatedImageView.init()
        let url2 =  Bundle.main.url(forResource: "home_platform_bottom_gif", withExtension: "gif")
        do{
            let data2 = try Data(contentsOf: url2!)
            let animatedImage2 = FLAnimatedImage.init(animatedGIFData: data2)
            self.bottomImageView?.animatedImage = animatedImage2
        }catch{}
        
        self.bottomBtn?.addSubview(self.bottomImageView!)
        self.bottomImageView?.snp.makeConstraints{(make) in
            make.width.equalTo(25)
            make.height.equalTo(14)
            make.centerX.equalToSuperview()
            make.bottom.equalToSuperview().offset(-10)
        }
        
//        self.bottomImageView = UIImageView.init()
//        self.bottomImageView?.image = UIImage.gif(name: "home_platform_bottom_gif")
//        // self.topBtn?.addTarget(self, action: #selector(actionToScrollTop), for: UIControl.Event.touchUpInside)
//        self.bgImageView?.addSubview(self.bottomImageView!)
//        self.bgImageView?.snp.makeConstraints{(make) in
//            make.width.equalToSuperview().offset(-12)
//            make.height.equalTo(24)
//            make.centerX.equalToSuperview()
//            make.bottom.equalToSuperview().offset(-3)
//        }
    }
    
    //滚动到顶部
    @objc func actionToScrollTop() -> Void {
        self.tableView.setContentOffset(CGPoint.init(x: 0, y: 0), animated: true)
    }
    
    //滚动到底部
    @objc func actionToScrollBottom() -> Void {
        self.tableView.setContentOffset(CGPoint.init(x: 0, y: self.tableView.contentSize.height-self.tableView.height), animated: true)
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        if scrollView == self.tableView {
            if (self.tableView.contentOffset.y <= 0)
            {
                //顶部
                print("到达顶部")
                self.topBtn?.isHidden = true
            }else{
                self.topBtn?.isHidden = false
            }
            
            if (self.tableView.contentSize.height - self.tableView.contentOffset.y-self.tableView.frame.size.height <=  0)
            {
                //底部
                print("到达底部")
                self.bottomBtn?.isHidden = true
            }else{
                self.bottomBtn?.isHidden = false
            }
        }
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return  self.arrayList?.count ?? 0
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 75
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        return UIView.init()
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 0
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell :PlatformViewCell? = tableView.dequeueReusableCell(withIdentifier: reuseIdentifierID as String) as? PlatformViewCell
        if cell == nil {
            cell =  PlatformViewCell(style: UITableViewCell.CellStyle.default, reuseIdentifier: reuseIdentifierID as String)
        }
        var isSelect = false
        if indexPath.row ==  self.selectIndex {
            isSelect = true
        }
       let dict = self.arrayList?[indexPath.row]
        cell?.setData(imgUrl: dict?["f_imgNameIOS"] as? String ?? "", selImgUrl: dict?["f_imgNameIOSed"] as? String ?? "", title: dict?["f_name"] as? String ?? "", isSelect: isSelect)
        return cell!
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        self.selectIndex = indexPath.row
        if platformSelectBlock != nil {
            platformSelectBlock!(indexPath.row)
        }
        self.tableView.reloadData()
    }
    func reload() -> Void {
        //
        let platforms = GlobalInfo.platformInfoData
        self.arrayList =  platforms
        self.tableView.reloadData()
    }
}
